package com.xiaozhengkeji.springboot.speed.api.v1;

import com.alibaba.fastjson.JSONArray;

import com.xiaozhengkeji.springboot.speed.bean.ResponseBean;
import com.xiaozhengkeji.springboot.speed.pojo.UserPoJo;
import com.xiaozhengkeji.springboot.speed.services.UserService;
import com.xiaozhengkeji.springboot.speed.utils.JWTUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.UUID;

@Slf4j
@RestController
@ApiOperation("用户API")
@RequestMapping("/v1/user")
public class UserApi {
    @Autowired
    private UserService userService;


    @GetMapping("/out")
    @ApiOperation("退出登陆")
    public void out(@RequestParam("token") String token) {
        if (userService.isToken(token)){
            String userName = JWTUtil.getUsername(token);
            userService.outToken(userName);
        }
    }

    @GetMapping("/info")
    @ApiOperation("需要在请求协议头添加Authorization: Token")
    @RequiresAuthentication
    public ResponseBean info(@RequestParam("token") String token) {
        String username = JWTUtil.getUsername(token);
        return new ResponseBean(200, "OK!", userService.getUser(username));
    }


    @PostMapping("/login")
    @ApiOperation("登陆")
    public ResponseBean login(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                              HttpServletRequest request) {
        UserPoJo userPoJo = userService.getUser(username);
        if (userPoJo == null) {
            return new ResponseBean(401, "登陆失败!", "用户名不存在!");
        }

        if (userPoJo.getPassword().equalsIgnoreCase(password)) {
            HashMap<Object, Object> objectHashMap = new LinkedHashMap<>();
            objectHashMap.put("token",JWTUtil.sign(username, password));
            objectHashMap.put("id",userPoJo.getId());
            return new ResponseBean(200, "登陆成功!", objectHashMap);
        }
        return new ResponseBean(401, "登陆失败!", "用户密码错误!");
    }


    @PostMapping("/register")
    @ApiOperation("注册")
    public ResponseBean register(@RequestParam("username") String username,
                                 @RequestParam("password") String password) {
        UserPoJo userPoJo = new UserPoJo();
        userPoJo.setRid(1);
        userPoJo.setUsername(username);
        userPoJo.setPassword(password);
        return new ResponseBean(200, "处理完成!", userService.regUser(userPoJo));
    }


    @GetMapping("/register/get")
    @ApiOperation("判断用户是否存在")
    public ResponseBean registerGetUser(@RequestParam("username") String username) {
        UserPoJo userPoJo = userService.getUser(username);
        if (userPoJo != null) {
            return new ResponseBean(200, "用户名存在", true);
        }
        return new ResponseBean(200, "用户名不存在", false);
    }
}
